<?php

namespace App\Models\News;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use App\Models\Template;
use App\Models\Site;
use App\Helper\Build\Build;
class NewsClass extends Model
{
    //
    use SoftDeletes;
    use ModelTree, AdminBuilder;
    
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    
        $this->setParentColumn('parent_id');
        $this->setOrderColumn('listorder');
        $this->setTitleColumn('name');
    }
	
	//封面模板
	public function template_index()
	{
		return $this->hasOne(Template::class, 'id', 'index_temp_id');
	}
	//内容模板
	public function template_cont()
	{
		return $this->hasOne(Template::class, 'id', 'cont_temp_id');
	}


	public static function build_list_class($id)
	{
		$Site = Site::default_site();
        $Build = new Build($Site);
		$model = self::where('index_temp_id', '>', 0)->whereNotNull('alias');
		if($id>0)
		{
			$info = $model->find($id);
			if($info)
			{
				return $Build->news_class($value);
			}
			return ['msg'=> '生成失败，请检查是否存在封面模版和别名'];

		}
        foreach ($model->get() as $key => $value) {
        	# code...
        	$Build->news_class($value);
        }
		return ['msg'=>'生成成功'];
	}
}
